<?php
class X3dAction extends ZMAction {
    public function main(){
        $app_url = $this->getAppPath();
        $modelurl = $app_url .C('MODELURL');
        $typeArr = array();
        $this->assign('app_url',$app_url);
        $this->assign('x3dcontent',$this->getDevX3d(0,$modelurl,'/0/',$typeArr));
        $this->assign('viewpoint',$this->getViewPoint());
        $this->assign('timeline',$this->getTimeLine());
        //$this->assign('lines',$this->getLines());
        $this->display('main');
    }
    
    public function test(){
        $app_url = $this->getAppPath();
        $modelurl = $app_url .C('MODELURL');
        $typeArr = array();
        $this->assign('app_url',$app_url);
        $this->assign('x3dcontent',$this->getDevX3d(0,$modelurl,'/0/',$typeArr));
        $this->assign('viewpoint',$this->getViewPoint());
        $this->assign('timeline',$this->getTimeLine());
        //$this->assign('lines',$this->getLines());
        $this->display('test');
    }
    
    protected function getViewPoint(){
        $model = M('Viewpoint');
        $res = $model->where('userid='.$_SESSION['user']['userid'])->find();
        if($res){
            return '<viewpoint position="' . $res['position'] . '" orientation="' . $res['orientation'] . '" id="aView"></viewpoint>';
        }
        return '<viewpoint id="aView"></viewpoint>';
    }
    
    protected function getTimeLine(){
        return '';
    }
      
    protected function getDevX3d($parentid,$modelurl,$spath,$typeArr){
        $sX3d = '';
        $actDev = A('Dev');
        $model = M('Dev');
        $modelDef = M('DevDef');
        $result = $model->where("parentid=$parentid and istemp=0")->select();
        foreach($result as $res){
            //获取模型URL
            $ddid = $res['ddid'];
            $devid = $res['devid'];
            $resDef = $modelDef->where("ddid=$ddid")->find();
            $devmodelurl = '';
            if($resDef){
                if(!empty($resDef['modelurl']))
                    $devmodelurl = $modelurl . $resDef['modelurl'];
            }
                        
            $sX3d .= "<Transform id='trans_$devid'";            
            //获取空间坐标
            $retTrans = $this->getDevTransform($devid);
            if($retTrans){
                if(!empty($retTrans['translation'])) $sX3d .= " translation='" . $retTrans['translation'] ."'";
                if(!empty($retTrans['center'])) $sX3d .= " center='" . $retTrans['center'] . "'";
                if(!empty($retTrans['rotation'])) $sX3d .= " rotation='" . $retTrans['rotation'] . "'";
                if(!empty($retTrans['scale'])) $sX3d .= " scale='" . $retTrans['scale'] . "'";
            }
            
            $sX3d .= ">";         
            
            $sX3d .= "<Group id='dev_$devid' spath='$spath$devid'>";
   
            if(!empty($devmodelurl)){ //有模型时才显示
            /*
                $key = array_search($devmodelurl,$typeArr);
                if($key===false){
                    $sX3d .= "<Inline DEF='m_".count($typeArr)."' url='$devmodelurl'></Inline>";  
                    $typeArr[] = $devmodelurl;
                }else{
                    $sX3d .= "<Inline USE='m_$key'></Inline>";
                }
                */
                $sX3d .= "<Inline url='$devmodelurl'></Inline>";
            }
            
            $childX3d = $this->getDevX3d($res['devid'],$modelurl,"$spath$devid/");
            if($childX3d!=''){
                $sX3d .= $childX3d;
            }
            
            $sX3d .= "</Group>";
            $sX3d .= "</Transform>"; 
            
        }
        return $sX3d;
    }
        
    protected function x3d($modelurl,$typeArr,$id){
        $sRet = '';
        $Model = M('Device');
        $ModelType = M('DeviceTypeDef');
        $where['parentid'] = $id;
        $result = $Model->where($where)->select();
        foreach($result as $row){
            $devid = $row['devid'];
            $typeid = $row['typedefid'];
            $sRet .= '<Group id="dev_' . $devid .'">';
            $sRet .= "<Transform translation='" . $row['tx'] . " " . $row['ty'] . " " . $row['tz'] . "' rotation='" . $row['rx'] . " " . $row['ry'] . " " . $row['rz'] . " " . $row['ra'] . "'>";
            if(in_array($typeid,$typeArr)){ //如果有了的话
                $sRet .= "<Inline USE='t_$typeid'>";
            }else{
                $resType = $ModelType->where("typedefid=$typeid")->find();
                if($resType){
//                    $sRet .= "<Inline DEF='t_$typeid' url='$modelurl" . $resType['x3durl']. "'>";
                    $sRet .= "<Inline url='$modelurl" . $resType['x3durl']. "'>";
//                    $typeArr[] =$typeid;
                }
            }
            $sRet .= "</Transform>";
            $sRet .= $this->x3d($modelurl,$typeArr,$devid);
            $sRet .= "</Group>";
        }
        return $sRet;
    }
    
    public function dev(){
        $devid = $_REQUEST['id'];
        $app_url = dirname(__APP__);
        $app_url = str_replace("\\","/",$app_url);
        if(substr($app_url, -1, 1)!='/') $app_url .= '/';
        $this->assign('app_url',$app_url);
        
        $Model = M('Device');
        $where['devid'] = $devid;
        $result = $Model->where($where)->find();
        if($result){
            $typeid = $result['typedefid'];
            $ModelType = M('DeviceTypeDef');
            $resType = $ModelType->where("typedefid=$typeid")->find();
            $this->assign('device',$result);
            $this->assign('typeinfo',$resType);
        }
        
        $resChild = $Model->where("parentid=$devid")->select();
        $this->assign('children',$resChild);
           
        $this->display('device');
    }
    
    public function devinfo(){
        $devid = $_REQUEST['devid'];
        $Model = M('Device');
        $where['devid'] = $devid;
        $result = $Model->where($where)->find();
        if($result){
            foreach($result as &$row){
                //查找设备属性
                $ModelAttr = D('AttrView');
                $res = $ModelAttr->where($where)->select();
                if($res){
                    //属性格式化
                }else{
                    $res = array();
                }
                $row['attr'] = $res;
            }
        }else{
            $result = array();
        }
        return $result;
    }
    
    
    public function saveVP(){
        $model = M('Viewpoint');
        $data = $model->create();
        $data['userid'] = $_SESSION['user']['userid']; 
        $model->add($data,array(),true);
        $this->success('ok');
    }
    
    /**
    * 获取连接线
    * 
    */
    protected function getLines(){
        $modLine = M('Lines');
        $modLineXYZ = M('LinesXyz');
        $sLine = "<Group id='line_all' render='true'>";
        
        $res = $modLine->select();
        foreach($res as $row){
            $lineid = $row['lineid'];
            $color = '0 0 0';
            if($row['linetype']==1){
                $color = '0.5 0.5 0.5';
            }else if($row['linetype']==4){
                $color = '1 1 0';
            }
            
            $map['lineid'] = $lineid;
            $resXyz = $modLineXYZ->where($map)->order('ordernum,lid')->select();
            $i = 0;
            $coordIndex = "";
            $point = "";
            foreach($resXyz as $row){
                $coordIndex .= $i . ' ';
                $point .= $row['x'] . ' ' . $row['y'] . ' '. $row['z'] .' ';
                $i++;
            }
            
            $sLine .= "<Shape id='ln_$lineid'>";
            $sLine .= "<IndexedLineSet coordIndex='$coordIndex'>";
            $sLine .= "<Coordinate point='$point'/>";      
            $sLine .= "</IndexedLineSet>";
            $sLine .= "<Appearance>";
            $sLine .= "<Material emissiveColor='$color'/>";
            $sLine .= "<LineProperties linetype='1' linewidthScaleFactor='2'></LineProperties>";
            $sLine .= "</Appearance>";
            $sLine .= "</Shape>";
        }
        
        $sLine .= "</Group>";
        return $sLine;
    }
    
}
?>
